//
//  MTNewUserInfoVC.m
//  met
//
//  Created by 李晓俊 on 2019/6/25.
//  Copyright © 2019 李晓俊. All rights reserved.
//

#import "MTNewUserInfoVC.h"
#import "MTNewMineHeadView.h"
#import "MTNewMineCell.h"
#import "MTNewMineView.h"
#import "MTEditDataVC.h"
#import "MTTicketVC.h"
#import "MTNewMineTextView.h"
#import "MTNewEditQuestionVC.h"
#import "MTAngelVC.h"
#import "MTMapVC.h"
#import "MTMarkDetailVC.h"

#import "MTQuestionAnswerVC.h"
@interface MTNewUserInfoVC ()
@property (nonatomic,strong) UITableView *tableview;
@property (nonatomic,strong) NSMutableArray *dataArr;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic,strong) MTNewMineHeadView *headView;
@property (nonatomic,strong) UserInfo *userModel;

@end

@implementation MTNewUserInfoVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self wr_setNavBarBackgroundAlpha:0];
    self.tableview = [[UITableView alloc] initWithFrame: CGRectMake(0, -NavHeight, kScreenWidth, self.view.frame.size.height +NavHeight) style:UITableViewStyleGrouped];
    self.tableview.dataSource = self;
    self.tableview.delegate = self;
    [self.view addSubview:self.tableview];
    //    self.tableview.tableFooterView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 30)];
    self.tableview.backgroundColor = KBackColor;
    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.dataArr = [NSMutableArray new];
    
    self.headView = [[MTNewMineHeadView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 316)];
    self.tableview.tableHeaderView = self.headView;
    
    self.headView.dituView.hidden = YES;
    self.headView.shezhiView.hidden = YES;
    self.headView.nameView.hidden = YES;
   
    self.view.backgroundColor = KWhiteColor;
    self.tableview.backgroundColor = KWhiteColor;

    [self getUserInfo];
    [self getStory];
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    DLog(@"%f",self.tableview.contentOffset.y);
    float y = self.tableview.contentOffset.y;
    
    if (self.tableview.contentOffset.y <= -NavHeight) {
        self.tableview.bounces = NO;
        [self wr_setNavBarBackgroundAlpha:0];
    }
    else if (self.tableview.contentOffset.y >= -NavHeight){
        self.tableview.bounces = YES;
        float alpha = 1 - ( (0 - y)/NavHeight);
        [self wr_setNavBarBackgroundAlpha:alpha];
    }
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.dataArr.count == 0) {
        return 0;
    }
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MTNewMineCell *cell = [MTNewMineCell cellWithTableView:tableView];
    for (int i = 0; i < self.dataArr.count; i ++) {
        int x = i % 2;
        int y = i / 2;
        CGFloat wid  = (kScreenWidth-20-10)/2.0;
        MTNewMineView *v = [[MTNewMineView alloc] initWithFrame:CGRectMake(x*(wid+10)+5, 333 * y, wid, 323)];
        [cell.bgView addSubview:v];
        
        UserStoryYinJiModel *model = [self.dataArr objectAtIndex:i];
        v.model = model;
        v.selectChoose = ^(UserStoryYinJiModel * dic) {
            if ([[NSString stringWithFormat:@"%@",dic.objectType]isEqualToString:@"1"]) {
                //印记
                [self getYinjiDetail:[NSString stringWithFormat:@"%@",dic.objectId]];
            }else {
                [self getStoryDetail:[NSString stringWithFormat:@"%@",dic.objectId]];
            }
        };
    }
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    int y = (self.dataArr.count - 1) / 2;
    
    return 333 * (y+1);
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 0.01f;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *v = [UIView new];
    return v;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01f;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [UIView new];
    return view;
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    // 设置导航栏默认的背景颜色
    
    [IQKeyboardManager sharedManager].enableAutoToolbar = NO;
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [IQKeyboardManager sharedManager].enableAutoToolbar = YES;
   
}
- (void)getUserInfo{
    //    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    AccountService *acc = [AccountService getObject];
    acc.httpRequestResultBlock = ^(id data,int code){
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if (code == 1) {
            self.userModel = data;
           
            
            [self.headView.headImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",self.userModel.portraitMap]] placeholderImage:[UIImage imageNamed:@"男（未选中）"]];

            [self.headView.dituImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",self.userModel.mapImage]] placeholderImage:[UIImage imageNamed:@"图层 36"]];
            
            self.headView.name.text = [NSString stringWithFormat:@"%@",self.userModel.nickName];
            self.headView.chepiaoCount.text = [NSString stringWithFormat:@"%@",self.userModel.ticket];
            
            [self.tableview reloadData];
        }else {
            [self.view maketishiToast:data];
        }
    };
    [acc findOtherUserInfo:self.userId];
}
- (void)getStory{
    AccountService *acc = [AccountService getObject];
    acc.httpRequestResultBlock = ^(id data,int code){
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if (code == 1) {
            self.dataArr = [NSMutableArray arrayWithArray:data];
            if (self.dataArr.count > 0) {
                NSUserDefaults *dataUser = [NSUserDefaults standardUserDefaults];
                [dataUser setObject:@"1" forKey:@"fabu"];
                [dataUser synchronize];
            }
            [self.tableview reloadData];
        }else {
            [self.view maketishiToast:data];
        }
    };
    [acc storyFindStoryAndIPByUsersId:self.userId];
}
- (void)getStoryDetail:(NSString *)storyId{
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    AccountService *acc = [AccountService getObject];
    acc.httpRequestResultBlock = ^(id data,int code){
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if (code == 1) {
            //用户
            MTQuestionAnswerVC *vc = [[MTQuestionAnswerVC alloc] init];
            vc.dataArr = [data objectForKey:@"questionList"];
            vc.leftName = [NSString stringWithFormat:@"%@",[data objectForKey:@"nickName"]];
            vc.rightName = KUSER.nickName;
            vc.storyId = [NSString stringWithFormat:@"%@",[data objectForKey:@"storyId"]];
            vc.storyVersion = [NSString stringWithFormat:@"%@",[data objectForKey:@"storyVersion"]];
            vc.createUsersId = [NSString stringWithFormat:@"%@",[data objectForKey:@"createUsersId"]];
            vc.sex = [NSString stringWithFormat:@"%@",[data objectForKey:@"sex"]];

            vc.leftUrl = [NSString stringWithFormat:@"%@",[data objectForKey:@"portraitIm"]];
            vc.rightUrl = [NSString stringWithFormat:@"%@",KUSER.portraitIm];
            vc.sceneUrl = [NSString stringWithFormat:@"%@",[data objectForKey:@"sceneUrl"]];
            
            [self.navigationController pushViewController:vc animated:YES];
            
        }else {
            [self.view maketishiToast:data];
        }
    };
    [acc findStoryByTicketStoryId:storyId];
}
- (void)getYinjiDetail:(NSString *)storyId{
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    AccountService *acc = [AccountService getObject];
    acc.httpRequestResultBlock = ^(id data,int code){
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if (code == 1) {
            MTMarkDetailVC *vc = [[MTMarkDetailVC alloc] init];
            vc.model = data;
            vc.isLook = @"1";
            [self.navigationController pushViewController:vc animated:YES];
        }else {
            [self.view maketishiToast:data];
        }
    };
    [acc imprintFind:storyId];
}








@end
